<!-- UserLayout.vue 这是usercenter专用，不要动谢谢-->
<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <el-header class="app-header">
      <div class="header-left">
        <h2 class="logo">计算机考试训练系统</h2>
      </div>
      <div class="header-right">
        <el-dropdown @command="handleCommand">
          <span class="user-dropdown">
            <el-avatar :size="32" :src="userAvatar" />
            <span class="username">{{ username }}</span>
            <el-icon><ArrowDown /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="userCenter">
                <el-icon><User /></el-icon>个人中心
              </el-dropdown-item>
              <el-dropdown-item command="setting">
                <el-icon><Setting /></el-icon>设置
              </el-dropdown-item>
              <el-dropdown-item divided command="logout">
                <el-icon><SwitchButton /></el-icon>退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <!-- 主体内容区 -->
    <div class="main-container">
      <!-- 侧边菜单 -->
      <el-aside width="220px" class="app-aside">
        <el-menu
          :default-active="activeMenu"
          class="app-menu"
          :router="true"
          :collapse="isCollapse"
        >
          <el-menu-item index="/home/user-center">
            <el-icon><HomeFilled /></el-icon>
            <template #title>首页</template>
          </el-menu-item>

          <el-sub-menu index="user-center">
            <template #title>
              <el-icon><User /></el-icon>
              <span>个人中心</span>
            </template>
            <el-menu-item index="/home/user-center">
              <el-icon><DataLine /></el-icon>
              <span>概览</span>
            </el-menu-item>
            <el-menu-item index="/home/user-center/favorites">
              <el-icon><Star /></el-icon>
              <span>我的收藏</span>
            </el-menu-item>
            <el-menu-item index="/home/user-center/progress">
              <el-icon><Timer /></el-icon>
              <span>学习进度</span>
            </el-menu-item>
            <el-menu-item index="/home/user-center/test">
              <el-icon><Document /></el-icon>
              <span>测试记录</span>
            </el-menu-item>
            <el-menu-item index="/home/user-center/message">
              <el-icon><Message /></el-icon>
              <span>消息中心</span>
            </el-menu-item>
            <el-menu-item index="/home/user-center/feedback">
              <el-icon><ChatDotRound /></el-icon>
              <span>留言反馈</span>
            </el-menu-item>
            <el-menu-item index="/home/user-center/setting">
              <el-icon><Setting /></el-icon>
              <span>设置</span>
            </el-menu-item>
          </el-sub-menu>

          <el-menu-item index="/home/user-communication">
            <el-icon><ChatLineRound /></el-icon>
            <template #title>用户交流</template>
          </el-menu-item>

          <el-menu-item index="/home/wrong-questions">
            <el-icon><Warning /></el-icon>
            <template #title>错题本</template>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 内容区域 -->
      <el-main class="app-main">
        <router-view v-slot="{ Component }">
  <transition name="fade-transform" mode="out-in">
    <component :is="Component" :key="$route.fullPath" />
  </transition>
</router-view>
      </el-main>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
  ArrowDown,
  User,
  Setting,
  SwitchButton,
  HomeFilled,
  Star,
  Timer,
  Document,
  Message,
  ChatDotRound,
  ChatLineRound,
  Warning,
  DataLine
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

// 用户信息
const username = ref('用户名')
const userAvatar = ref('')

// 菜单折叠状态
const isCollapse = ref(false)

// 当前激活的菜单
const activeMenu = computed(() => {
  return route.path
})

// 处理下拉菜单命令
const handleCommand = (command) => {
  switch (command) {
    case 'userCenter':
      router.push('/home/user-center')
      break
    case 'setting':
      router.push('/home/user-center/setting')
      break
    case 'logout':
      localStorage.removeItem('user-token')
      router.push('/login')
      break
  }
}
</script>

<style scoped>
.app-container {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.app-header {
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.header-left .logo {
  margin: 0;
  font-size: 20px;
  color: #409EFF;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin: 0 8px;
  color: #606266;
}

.main-container {
  margin-top: 60px;
  display: flex;
  min-height: calc(100vh - 60px);
}

.app-aside {
  background-color: #fff;
  box-shadow: 2px 0 8px 0 rgba(29, 35, 41, 0.05);
  position: fixed;
  left: 0;
  top: 60px;
  bottom: 0;
  z-index: 999;
}

.app-menu {
  border-right: none;
  height: 100%;
}

.app-main {
  margin-left: 220px;
  padding: 20px;
  min-height: calc(100vh - 60px);
}

/* 路由切换动画 */
.fade-transform-enter-active,
.fade-transform-leave-active {
  transition: all 0.3s;
}

.fade-transform-enter-from {
  opacity: 0;
  transform: translateX(-30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  display: flex;
  align-items: center;
}

:deep(.el-menu-item .el-icon),
:deep(.el-sub-menu__title .el-icon) {
  margin-right: 8px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 8px;
}
</style>
